package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.mapper.TbContentCategoryMapper;
import com.usian.pojo.TbContentCategory;
import com.usian.service.ContentCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {
    @Resource
    private TbContentCategoryMapper tbContentCategoryMapper;
    //根据当前节点 ID 查询子节点 查类目
    @Override
    public List<TbContentCategory> selectContentCategoryByParentId(Long parentId) {
        QueryWrapper<TbContentCategory> tbContentCategoryQueryWrapper = new QueryWrapper<>();
        tbContentCategoryQueryWrapper.eq("parent_id",parentId);
        List<TbContentCategory> list = tbContentCategoryMapper.selectList(tbContentCategoryQueryWrapper);
        return list;
    }
//添加分类内容
    @Override
    public Integer insertContentCategory(TbContentCategory tbContentCategory) {
        //1、添加内容分类0是false  1是true
        tbContentCategory.setUpdated(new Date());
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setIsParent(false);
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setStatus(1);
        Integer contentCategoryNum =
                tbContentCategoryMapper.insert(tbContentCategory);
        //2、如果他爹不是爹，要把他爹改成爹
        //2.1、查询当前新节点的父节点
        TbContentCategory contentCategory = tbContentCategoryMapper.selectByPrimaryKey(tbContentCategory.getParentId());
        //2.2、判断当前父节点是否是叶子节点
        if (!contentCategory.getIsParent()) {
            contentCategory.setIsParent(true);
            contentCategory.setUpdated(new Date());
            this.tbContentCategoryMapper.updateByPrimaryKey(contentCategory);
        }
        return contentCategoryNum;
    }
//删除节点
    @Override
    public Integer deleteContentCategoryById(Long categoryId) {
        //判断是否是父节点
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(categoryId);
        //父节点不能删除
        if (tbContentCategory.getIsParent()){
            return 0;
        }
        //删除节点
        tbContentCategoryMapper.deleteByPrimaryKey(categoryId);
        //找当前节点的兄弟节点
        QueryWrapper<TbContentCategory> tbContentCategoryQueryWrapper = new QueryWrapper<>();
        tbContentCategoryQueryWrapper.eq("parent_id",tbContentCategory.getParentId());
        List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectList(tbContentCategoryQueryWrapper);
        if (tbContentCategories.size()==0){
            TbContentCategory tbContentCategory1 = new TbContentCategory();
            tbContentCategory1.setId(tbContentCategory.getParentId());
            tbContentCategory1.setIsParent(false);
            tbContentCategory1.setUpdated(new Date());
            tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory1);
        }
        //如果没有兄弟节点把他的父节点改为子节点也就是非父节点
        return 200;
    }
    //修改节点
    @Override
    public Integer updateContentCategory(TbContentCategory tbContentCategory) {
        tbContentCategory.setUpdated(new Date());
        return tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
    }

}
